package com.ruoyi.article.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.article.domain.*;
import com.ruoyi.article.domain.vo.ArticleParam;
import com.ruoyi.article.domain.vo.Label;
import com.ruoyi.article.service.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.service.ISysNoticeService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/api/userApi")
public class ArticleApi extends BaseController{

    @Autowired
    private IZArticleService zArticleService;

    @Autowired
    private ISysNoticeService noticeService;

    @Autowired
    private IZLabelService zLabelService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IZArticleReviewService zArticleReviewService;

    @Autowired
    private IZPraiseLogService zPraiseLogService;

    @Autowired
    private IZBugService zBugService;
    /**
     * 查询轮播图文章
     * @return
     */
    @GetMapping("/listArticleTop")
    public AjaxResult listArticleTop()
    {
        return success(zArticleService.selectTopArticle());
    }

    /**
     * 查询通知列表
     * @return
     */
    @GetMapping("/selectNot")
    public AjaxResult selectNot()
    {
        SysNotice sysNotice = new SysNotice();
        sysNotice.setNoticeType("1");
        List<SysNotice> sysNotices = noticeService.selectNoticeList(sysNotice);
        return success(sysNotices.get(0));
    }


    /**
     * 查询标签列表
     * @return
     */
    @GetMapping("/selectNavi")
    public AjaxResult selectNavi()
    {

        LambdaQueryWrapper<ZLabel> zLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        zLabelLambdaQueryWrapper.eq(ZLabel::getIsStart,"0");
        List<ZLabel> list = zLabelService.list(zLabelLambdaQueryWrapper);
        List<Label> collect = list.stream().map(item -> new Label(item.getId(), item.getName())).collect(Collectors.toList());
        return success(collect);
    }

    /**
     * 根据文章标题查询文章列表
     */
    @GetMapping("/getArticleList")
    public TableDataInfo list(String title,Integer pageCurrent,Integer pageSize)
    {
        List<ZArticle> list = zArticleService.selectArticleByTitle(title,pageCurrent,pageSize);
        long total = zArticleService.selectCountByTitle(title);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(total);
        return rspData;
    }

    /**
     * 获取所属标签下的文章
     */
    @GetMapping(value = "/listByLabelId")
    public TableDataInfo getArticleListByLabelId(ArticleParam articleReview)
    {
        return zArticleService.getArticleListByLabelId(articleReview);
    }

    /**
     * 获取文章详细信息
     */
    @GetMapping(value = "/articleById/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(zArticleService.getByIdArticle(id));
    }

    /**
     * 获取用户详细信息
     */
    @GetMapping(value = "/getInfo")
    public AjaxResult getUserInfo(HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        return success(loginUser);
    }

    /**
     * 查询评论列表
     */
    @GetMapping("/getReview")
    public TableDataInfo getReview(ZArticleReview zArticleReview)
    {

        TableDataInfo reviewTree = zArticleReviewService.selectReviewTreeUser(zArticleReview);
        return reviewTree;
    }



    /**
     * 新增一级评论
     */
    @PostMapping("/addReview")
    public AjaxResult add(@RequestBody ZArticleReview zArticleReview)
    {
        return toAjax(zArticleReviewService.insertReview(zArticleReview));
    }

    /**
     * 新增二级评论
     */
    @PostMapping("/saveReview")
    public AjaxResult saveReview(@RequestBody ZArticleReview zArticleReview)
    {
        return toAjax(zArticleReviewService.saveReview(zArticleReview));
    }


    /**
     * 删除评论
     */
    @DeleteMapping("/delReply/{id}")
    public AjaxResult delReply(@PathVariable Long id)
    {
        return toAjax(zArticleReviewService.removeByIdTree(id));
    }


    /**
     * 新增用户点赞日志记录
     */
    @PostMapping("/addReviewLog")
    public AjaxResult addReviewLog(@RequestBody ZArticleReview zArticleReview)
    {
        //这里得到的是评论记录的id
        Long id = zArticleReview.getId();
        Long contentId = zArticleReview.getContentId();
        Integer status = zArticleReview.getStatus();

        return toAjax(zPraiseLogService.insertReviewLog(id,contentId,status));
    }


    /**
     * 查询点赞日志表
     */
    @GetMapping("/getLikeList")
    public TableDataInfo getLikeList(ZArticleReview zArticleReview)
    {
        Long contentId = zArticleReview.getContentId();
        TableDataInfo reviewTree = zPraiseLogService.selectRevieLikeList(contentId);
        return reviewTree;
    }

    @GetMapping("/listAny")
    public AjaxResult listAny()
    {
        List<ZLabel> list = zLabelService.list();
        return success(list);
    }

    /**
     * 新增文章
     */
    @PostMapping("/saveArticle")
    public AjaxResult add(@RequestBody ZArticle zArticle) throws UnknownHostException {
        return success(zArticleService.insertZArticleAll(zArticle));
    }

    /**
     * 查询当前用户文章列表
     */
    @GetMapping("/listArticleByUserId")
    public TableDataInfo listArticleByUserId(ZArticle zArticle)
    {
        TableDataInfo tableDataInfo = zArticleService.listArticleByUserId(zArticle);
        return tableDataInfo;
    }

    /**
     * 查询当前用户文章收藏列表
     */
    @GetMapping("/listArticleCollect")
    public TableDataInfo listArticleCollect(ZArticle zArticle)
    {
        TableDataInfo tableDataInfo = zArticleService.listArticleCollect(zArticle);
        return tableDataInfo;
    }


    /**
     * 删除文章
     */
    @DeleteMapping("/article/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(zArticleService.deleteZArticleByIds(ids));
    }


    /**
     * 新增用户文章收藏，日志记录
     */
    @PostMapping("/addCollectAndLike")
    public AjaxResult addReviewLog(@RequestBody ZPraiseLog Article)
    {
        //这里得到的是文章id
        Long articleId = Article.getArticleId();
        String status = Article.getStatus();
        return toAjax(zPraiseLogService.insertReviewLog(articleId,status));
    }

    //查询文章是否被收藏
    @GetMapping("/getIsCollect/{articleId}")
    public AjaxResult isCollect(@PathVariable Long articleId)
    {
        Boolean flag = zPraiseLogService.getIsCollect(articleId);
        return success(flag);
    }

    //查询文章是否被点赞
    @GetMapping("/getIsLoveStar/{articleId}")
    public AjaxResult getIsLoveStar(@PathVariable Long articleId)
    {
        Boolean flag = zPraiseLogService.getIsLoveStar(articleId);
        return success(flag);
    }
    /**
     * 新增用户文章点赞，日志记录
     */
    @PostMapping("/addArticleLike")
    public AjaxResult addArticleLike(@RequestBody ZPraiseLog Article)
    {
        //这里得到的是文章id
        Long articleId = Article.getArticleId();
        //前端传递的是'true'收藏 或'false'取消收藏
        String status = Article.getStatus();
        return toAjax(zPraiseLogService.insertReviewLogArticleLike(articleId,status));
    }

    /**
     * 新增bug描述
     */
    @PostMapping("/article/bug")
    public AjaxResult add(@RequestBody ZBug zBug)
    {
        return toAjax(zBugService.save(zBug));
    }

    /**
     * 获取通知公告列表
     */
    @GetMapping("/notice/list/{userId}")
    public TableDataInfo list(@PathVariable Long userId)
    {

        List<Character> stringArray = Arrays.asList('1', '2');

        Map<String, Object> params = new HashMap<>();
        params.put("noticeTypes", stringArray);
        params.put("userId", userId);
        List<SysNotice> list = noticeService.selectNoticeByTypeList(params);
        return getDataTable(list);
    }


}


